/*
 * 文 件 名 : AlarmActivity.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2013-2-4
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui.plan;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.fire8.dolphin.db.PlanDAO;
import com.fire8.dolphin.model.Plan;
import com.fire8.dolphin.ui.MainActivity;
import com.fire8.dolphin.ui.R;
import com.fire8.dolphin.utils.LogUtil;

/**
 * 计划提醒页面
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2013-2-4]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class AlarmActivity extends Activity
{

    private TextView tvAlarm;
    private Button btnFinish;
    private Button btnClose;
    private Button btnOpen;
    private AlarmManager aManager;//闹钟管理器
    private String strAlarm;
    private Integer planId;
    private PlanDAO pdDAO;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        aManager = (AlarmManager)getSystemService(Service.ALARM_SERVICE);
        pdDAO = new PlanDAO(AlarmActivity.this);
        Intent intent = getIntent();
        strAlarm = intent.getStringExtra("title");
        planId = intent.getIntExtra("planId", -1);
        setContentView(R.layout.alarm);
        tvAlarm = (TextView)findViewById(R.id.tv_alarm);
        btnFinish = (Button)findViewById(R.id.btn_finish);
        btnClose = (Button)findViewById(R.id.btn_close);
        btnOpen = (Button)findViewById(R.id.btn_open);
        tvAlarm.setText(strAlarm);
        btnFinish.setOnClickListener(new ButtonClickListener());
        btnClose.setOnClickListener(new ButtonClickListener());
        btnOpen.setOnClickListener(new ButtonClickListener());
    }
    
    class ButtonClickListener implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            //删除闹钟
            PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), strAlarm.hashCode(), null, PendingIntent.FLAG_UPDATE_CURRENT);
            aManager.cancel(pi);
            if(v.getId() == btnClose.getId())
            {
                //关闭窗口
                
            }else if(v.getId() == btnOpen.getId())
            {
                //打开海豚知音
                if(!MainActivity.isOpen())
                {
                    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(intent);
                }
            }else
            {
                //将计划状态修改为完成
                Plan plan = pdDAO.getPlan(Long.valueOf(planId));
                if(null!=plan){
                    plan.setFinish(Plan.FINISH_YES);
                    pdDAO.update(plan);
                    LogUtil.d("完成计划");
                }
            }
            AlarmActivity.this.finish();
        }
        
    }
}
